package com.tanxi521.com.thenorthtitle;

import com.tanxi521.com.thenorthtitle.command.TheNorthTitleCmd;
import com.tanxi521.com.thenorthtitle.event.TnTitleListener;
import com.tanxi521.com.thenorthtitle.placeholder.TheNorthTitlePlaceHolder;
import com.tanxi521.com.thenorthtitle.sql.dao.TnTitleBaseInfoMapper;
import com.tanxi521.com.thenorthtitle.util.PointApi;
import com.tanxi521.com.thenorthtitle.util.VaultApi;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;

public final class TheNorthTitle extends JavaPlugin {

    public static TheNorthTitle instance;

    public static List<String> allTitleName;

    public static TheNorthTitle getInstance() {
        return instance;
    }

    @Override
    public void onLoad() {
        instance = this;
    }

    @Override
    public void onEnable() {
        allTitleName = TnTitleBaseInfoMapper.selectAll();
        getLogger().info("已加载 " + allTitleName.size() + " 个称号");
        Bukkit.getPluginCommand("tntitle").setExecutor(new TheNorthTitleCmd());
        Bukkit.getPluginManager().registerEvents(new TnTitleListener(), this);
        getLogger().info("已注册监听器");
        if (VaultApi.setupEconomy())
            getLogger().info("已兼容经济插件 vault");
        else {
            getLogger().info("未发现插件Vault, 插件即将卸载");
            instance.setEnabled(false);
        }
        try {
            PointApi.hookPlayerPoints();
            getLogger().info("已兼容经济插件 PlayerPoint");
        } catch (ClassNotFoundException e) {
            getLogger().info("未发现点券插件, 插件即将卸载");
            instance.setEnabled(false);
        }

        if (PlaceholderAPI.isRegistered("title")) PlaceholderAPI.unregisterPlaceholderHook("title");
        if (new TheNorthTitlePlaceHolder().register()) getLogger().info("已兼容变量插件 PlaceHolderAPI");
        else getLogger().info("PlaceHolder注册失败，变量将无法正常显示");
        // Plugin startup logic
        getLogger().info("已成功载入");
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        if (PlaceholderAPI.unregisterPlaceholderHook("title"))
            getLogger().info("PlaceHolderAPI变量已注销");
    }
}
